package com.yzj.micro.demo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yzj.demo.common.pojo.po.StockPO;
import com.yzj.micro.demo.stock.mapper.StockMapper;
import com.yzj.micro.demo.stock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;

    @Override
    public Boolean reduce(String productCode, Integer count) {
        //1.扣减库存 原库存1000个 count扣减掉
        //update stock_tbl set stock=stock-#{count} where product_code=#{productCode}
        UpdateWrapper<StockPO> updateWrapper=new UpdateWrapper<>();//和queryWrapper区别
        //可以封装set语句 queryWrapper只能封装where条件
        updateWrapper.setSql("stock=stock-"+count);
        updateWrapper.eq("product_code",productCode);
        //入参的第一个值 是一个StockPO对象,使用对象的非空属性拼接set
        int update = stockMapper.update(null, updateWrapper);
        return update>0;
    }
}
